﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Deployment.BuildManager.ExternalProcess.CreateSetupFile
{
    public class Process
    {
        public static readonly Process Instance = new Process();
        public event Action<string> OutputDataReceived;
        public event Action Started;
        public event Action<int> Finalized;

        public void StartSetupProcess(ProcessStartInfo startInfo)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();

            process.StartInfo = startInfo;

            process.Start();

            if (Started != null)
            {
                Started();
            }

            process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);

            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            process.WaitForExit();

            if (Finalized != null)
            {
                Finalized(process.ExitCode);
            }
        }

        private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (OutputDataReceived != null)
            {
                OutputDataReceived(e.Data);
            }
        }
    }
}
